package assign3;
/**
* Title:
* Description: Assignment 3.  Has a default list of "Ellipses" and you can sort them four ways.  It will save one file and 
* you can open the last one saved (the one currently in the folder with the right name.
* @email Benjamin.Hosch@my.uwrf.edu
* @date 12/2/11
* @team Benjamin Hosch, Tyler Heil, John Feia
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;


/**
 * The class that sets up the whole GUI interface.
 * Sets up size of GUI, All Variables Needed, and implements them all.
 * Loads up a default list of 22 Ellipse.
 * Sets up all Needed Buttons and Menus.
 */
public class EllipseListGUI extends JFrame implements ActionListener, Serializable {

	private static final long serialVersionUID = 1L;
	private final int FRAME_X = 100, FRAME_Y = 100, FRAME_WIDTH = 460,
			FRAME_HEIGHT = 350;
	private final int SCROLL_X = 30, SCROLL_Y = 30, SCROLL_WIDTH = 400,
			SCROLL_HEIGHT = 200;
	private final int SORTMA_X = 30, SORTMiA_X = 130, SORTP_X = 230,
			SORTA_X = 330, SORTMA_Y = 235, SORTMA_WIDTH = 100,
			SORTMA_HEIGHT = 30;
	private String SORTMA_STRING = "Major Axis";
	private String SORTMiA_STRING = "Minor Axis";
	private String SORTP_STRING = "Perimeter";
	private String SORTA_STRING = "Area";
	private JList listview;
	private JScrollPane scroll;

	private JButton sortMA;
	private JButton sortMiA;
	private JButton sortP;
	private JButton sortA;
	private DefaultListModel listmodel;
	private Vector<Ellipse> list;
	private JMenuItem newMenu;
	private JMenuItem saveMenu;
	private JMenuItem openMenu;
	private static Ellipse[] ell;
	
	EllipseListGUI(String title) {
		super(title);
		setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(null);

		Ellipse a = new Ellipse(22, 27, 34, 41);
		Ellipse b = new Ellipse(154, 82, 43, 54);
		Ellipse c = new Ellipse(95, 27, 5, 16);
		Ellipse d = new Ellipse(13, 31, 45, 54);
		Ellipse e = new Ellipse(12, 2, 3, 31);
		Ellipse f = new Ellipse(955, 25, 5, 16);
		Ellipse g = new Ellipse(24, 23, 4, 421);
		Ellipse h = new Ellipse(12, 32, 43, 544);
		Ellipse i = new Ellipse(5, 22, 54, 156);
		Ellipse j = new Ellipse(613, 34, 5, 564);
		Ellipse k = new Ellipse(142, 201, 43, 331);
		Ellipse l = new Ellipse(925, 212, 75, 146);
		Ellipse m = new Ellipse(12, 235, 94, 841);
		Ellipse n = new Ellipse(31, 34, 43, 954);
		Ellipse o = new Ellipse(895, 20, 55, 416);
		Ellipse p = new Ellipse(113, 19, 35, 554);
		Ellipse q = new Ellipse(12, 21, 2, 931);
		Ellipse r = new Ellipse(87, 25, 8, 816);
		Ellipse s = new Ellipse(2, 23, 354, 141);
		Ellipse t = new Ellipse(1, 2, 453, 544);
		Ellipse u = new Ellipse(905, 220, 5, 156);
		Ellipse v = new Ellipse(33, 1, 455, 564);

		
		Ellipse[] ell = { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v };

		list = new Vector<Ellipse>();
		for (Ellipse z : ell)
			list.add(z);

		listmodel = new DefaultListModel();
		for (Ellipse z : ell)
			listmodel.addElement(z);

		listview = new JList(listmodel);
		listview.setVisible(true);

		scroll = new JScrollPane(listview);
		scroll.setBounds(SCROLL_X, SCROLL_Y, SCROLL_WIDTH, SCROLL_HEIGHT);
		add(scroll);

		sortMA = new JButton(SORTMA_STRING);
		sortMA.addActionListener(this);
		sortMA.setBounds(SORTMA_X, SORTMA_Y, SORTMA_WIDTH, SORTMA_HEIGHT);
		add(sortMA);

		sortMiA = new JButton(SORTMiA_STRING);
		sortMiA.addActionListener(this);
		sortMiA.setBounds(SORTMiA_X, SORTMA_Y, SORTMA_WIDTH, SORTMA_HEIGHT);
		add(sortMiA);

		sortP = new JButton(SORTP_STRING);
		sortP.addActionListener(this);
		sortP.setBounds(SORTP_X, SORTMA_Y, SORTMA_WIDTH, SORTMA_HEIGHT);
		add(sortP);

		sortA = new JButton(SORTA_STRING);
		sortA.addActionListener(this);
		sortA.setBounds(SORTA_X, SORTMA_Y, SORTMA_WIDTH, SORTMA_HEIGHT);
		add(sortA);

		JMenuBar menuBar = new JMenuBar();

		JMenu filemenu = new JMenu("File");
		menuBar.add(filemenu);

		newMenu = new JMenuItem("New");
		newMenu.addActionListener(this);
		filemenu.add(newMenu);

		openMenu = new JMenuItem("Open");
		openMenu.addActionListener(this);
		filemenu.add(openMenu);

		saveMenu = new JMenuItem("Save");
		saveMenu.addActionListener(this);
		filemenu.add(saveMenu);

		setJMenuBar(menuBar);

		setVisible(true);
	}

	/**
	 * Instantiates the whole GUI.  Names it "My Ellipse Sort"
	 */
	
	public static void main(String[] args) {

		new EllipseListGUI("My Ellipse Sort");

	}
	
	/**
	 * Action Performed.  Will execute different code depending on which button is pressed.
	 * @require Buttoned Pressed
	 * @ensure Correct Code will be Executed cooresponding to button
	 * 
	 */

	public void actionPerformed(ActionEvent button) {
		Object source = button.getSource();
		if (source.equals(sortMA)) {
			if (sortMA.getText().equals(SORTMA_STRING)) {
				/*
				 * Sort
				 */
				Comparator<Ellipse> comparator = new MajorAxisComparator();
				Collections.sort(list, comparator);
				listmodel.removeAllElements();
				for (Ellipse s : list)
					listmodel.addElement(s);
				listview = new JList(listmodel);
				listview.setVisible(true);

			}

		}
		if (source.equals(sortMiA)) {
			if (sortMiA.getText().equals(SORTMiA_STRING)) {
				/*
				 * Sort
				 */
				Comparator<Ellipse> comparator = new MinorAxisComparator();
				Collections.sort(list, comparator);
				listmodel.removeAllElements();
				for (Ellipse s : list)
					listmodel.addElement(s);
				listview = new JList(listmodel);
				listview.setVisible(true);

			}

		}

		if (source.equals(sortP)) {
			if (sortP.getText().equals(SORTP_STRING)) {
				/*
				 * Sort
				 */
				Comparator<Ellipse> comparator = new PerimeterComparator();
				Collections.sort(list, comparator);
				listmodel.removeAllElements();
				for (Ellipse s : list)
					listmodel.addElement(s);
				listview = new JList(listmodel);
				listview.setVisible(true);

			}

		}

		if (source.equals(sortA)) {
			if (sortA.getText().equals(SORTA_STRING)) {
				/*
				 * Sort
				 */
				Comparator<Ellipse> comparator = new AreaComparator();
				Collections.sort(list, comparator);
				listmodel.removeAllElements();
				for (Ellipse s : list)
					listmodel.addElement(s);
				listview = new JList(listmodel);
				listview.setVisible(true);

			}

		}
		if (source.equals(newMenu)) {
			list.removeAllElements();
			listmodel.removeAllElements();
		}

		
		if (source.equals(saveMenu)){
			try {
				FileOutputStream fs = new FileOutputStream("Ellipses");
				ObjectOutputStream oos = new ObjectOutputStream(fs);
					oos.writeObject(list);
					oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (source.equals(openMenu)) {
			try {
				FileInputStream fs = new FileInputStream("Ellipses");
				ObjectInputStream ois = new ObjectInputStream(fs);
				listmodel.removeAllElements();
				Vector<Ellipse> list = (Vector<Ellipse>) ois.readObject();
				for (Ellipse s : list)
					listmodel.addElement(s);
				listview = new JList(listmodel);
				listview.setVisible(true);
				ois.close();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * An ellipse with integer dimensions.
	 * @require
	 * @ensure
	 */
	
	public class Ellipse implements Serializable{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		double majorAxis;
		double minorAxis;
		double perimeter;
		double area;

		public Ellipse(int mA, int MiA, int P, int A) {

			majorAxis = mA;
			minorAxis = MiA;
			perimeter = P;
			area = A;

		}

		public String toString() {
			return "" + majorAxis + ", " + minorAxis + ", " + perimeter + ", "
					+ area;
		}
		}
	
	
		/**
		 * Comparator for Major Axis
		 * @Require Major Axis button Pressed
		 * @Ensure Sort by Major Axis
		 */

	private static class MajorAxisComparator implements Comparator<Ellipse> {
		public int compare(Ellipse a, Ellipse b) {
			if(a.majorAxis - b.majorAxis > 0)
				return 1;
			if(a.majorAxis - b.majorAxis < 0)
				return -1;
			return 0;
		}

	}


	/**
	 * Comparator for Minor Axis
	 * @Require Minor Axis button Pressed
	 * @Ensure Sort by Minor Axis
	 */
	
	private static class MinorAxisComparator implements Comparator<Ellipse> {
		public int compare(Ellipse a, Ellipse b) {
			if(a.minorAxis - b.minorAxis > 0)
				return 1;
			if(a.minorAxis - b.minorAxis < 0)
				return -1;
			return 0;
		}

	}


	/**
	 * Comparator for Parameter
	 * @Require Parameter Button Pressed
	 * @Ensure Sort by Parameter
	 */
	
	
	private static class PerimeterComparator implements Comparator<Ellipse> {
		public int compare(Ellipse a, Ellipse b) {
			if(a.perimeter - b.perimeter > 0)
				return 1;
			if(a.perimeter - b.perimeter < 0)
				return -1;
			return 0;
		}

	}
	

	/**
	 * Comparator for Area
	 * @Require Area button Pressed
	 * @Ensure Sort by Area
	 */
	

	private static class AreaComparator implements Comparator<Ellipse> {
		public int compare(Ellipse a, Ellipse b) {
			if(a.area - b.area > 0)
				return 1;
			if(a.area - b.area < 0)
				return -1;
			return 0;
		}
	}
}
